package adapletplugin.editors.pages;

import objects.IObject;
import objects.MappingFile;
import objects.Relationship;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

import sections.ISection.Section;
import adapletplugin.dialogs.SingleQuestionDialog;
import adapletplugin.editors.MapFileEditor;
import adapletplugin.editors.MapTableContentProvider;
import adapletplugin.editors.MapTableLabelProvider;
import adapletplugin.editors.RelEditor;
import adapletplugin.editors.input.MapFileEditorInput;
import adapletplugin.editors.input.MapTableInput;

public class MapFileRelationshipPage extends FormPage
{

	private MappingFile fMap;
	private MapFileEditor fEditor;
	private ScrolledForm fForm;
	private FormToolkit fToolkit;

	private Button newButton;
	private IObject focusObject;
	private TableViewer tableViewer;
	private MapTableInput tableInput;

	private RelEditor relEditor;

	public MapFileRelationshipPage(FormEditor editor)
	{
		super(editor, "mapfile.relationships", "Relationships");

		fEditor = (MapFileEditor) editor;
		fMap = ((MapFileEditorInput) editor.getEditorInput()).getMapFile();
	}

	@Override
	protected void createFormContent(IManagedForm managedForm)
	{
		fForm = managedForm.getForm();
		fForm.setText("Relationship Information");

		fToolkit = managedForm.getToolkit();

		Composite body = fForm.getBody();
		body.setLayout(new GridLayout(1, true));

		Composite mainArea = fToolkit.createComposite(body, SWT.NULL);
		mainArea.setLayout(new GridLayout(2, true));
		mainArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Composite leftArea = fToolkit.createComposite(mainArea, SWT.NULL);
		leftArea.setLayout(new GridLayout(1, true));
		leftArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		newButton = fToolkit.createButton(leftArea, "New", SWT.PUSH);
		newButton.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		newButton.addSelectionListener(createButtonListener());

		tableViewer = new TableViewer(leftArea, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL);
		tableViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		tableViewer.addSelectionChangedListener(createTableListener());
		tableViewer.setContentProvider(new MapTableContentProvider());
		tableViewer.setLabelProvider(new MapTableLabelProvider());

		tableInput = new MapTableInput(fMap, Section.REL);
		tableViewer.setInput(tableInput);

		Composite rightArea = fToolkit.createComposite(mainArea, SWT.BORDER);
		rightArea.setLayout(new GridLayout(1, true));
		rightArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		ScrolledComposite composite = new ScrolledComposite(rightArea, SWT.V_SCROLL);
		relEditor = new RelEditor(composite, fToolkit, fEditor);

	}

	private ISelectionChangedListener createTableListener()
	{
		return new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event)
			{
				focusObject = (IObject) ((IStructuredSelection) tableViewer.getSelection()).getFirstElement();

				relEditor.updateData(focusObject);
			}
		};
	}

	private SelectionListener createButtonListener()
	{
		return new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e)
			{
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

				SingleQuestionDialog dialog = new SingleQuestionDialog(shell, "Create Relationship",
						"Enter a name for the new relationship.", "Relationship Name");

				dialog.create();
				dialog.open();

				if (dialog.getReturnCode() == Window.OK)
				{
					Relationship newRel = fMap.getRelationshipSection().createNewRelationship(dialog.getResultString());
					focusObject = newRel;
					tableViewer.refresh();
				}

				fEditor.refresh();
			}
		};
	}

}
